|
1
|
|
|
import React from "react"; |
|
2
|
|
|
import { useState, useEffect, useRef } from "react"; |
|
3
|
|
|
import { Searchbar, Map, MapSidebar } from "./../components"; |
|
4
|
|
|
import mapConfig from "../config/config.json"; |
|
5
|
|
|
import getCoordinates from "../models/nominatim"; |
|
6
|
|
|
import scooter from "../models/scooters"; |
|
7
|
|
|
import cities from "../models/cities"; |
|
8
|
|
|
import "../Map.css"; |
|
9
|
|
|
const startpoint = mapConfig.center; |
|
10
|
|
|
|
|
11
|
|
|
const MapOverview = () => { |
|
12
|
|
|
const [center, setCenter] = useState(startpoint); |
|
13
|
|
|
const [zoom, setZoom] = useState(14); |
|
14
|
|
|
const [searchPhrase, setSearchPhrase] = useState(""); |
|
15
|
|
|
const [isLive, setIsLive] = useState(false); |
|
16
|
|
|
const [features, setFeatures] = useState(); |
|
17
|
|
|
const [scooters, setScooters] = useState([]); |
|
18
|
|
|
const [cityData, setCityData] = useState([]); |
|
19
|
|
|
const timerRef = useRef(null); |
|
20
|
|
|
timerRef.current = isLive; |
|
21
|
|
|
|
|
22
|
|
|
useEffect(() => { |
|
23
|
|
|
fetchScooterData(); |
|
24
|
|
|
}, []); |
|
25
|
|
|
|
|
26
|
|
|
useEffect(() => { |
|
27
|
|
|
async function fetchCityData() { |
|
28
|
|
|
const res = await cities.getCities(); |
|
29
|
|
|
const data = res.cities; |
|
30
|
|
|
setCityData(data); |
|
31
|
|
|
} |
|
32
|
|
|
fetchCityData(); |
|
33
|
|
|
}, []); |
|
34
|
|
|
|
|
35
|
|
|
useEffect(() => { |
|
36
|
|
|
updateMap(); |
|
37
|
|
|
}, [isLive]); |
|
38
|
|
|
|
|
39
|
|
|
async function fetchScooterData() { |
|
40
|
|
|
const res = await scooter.getScooters(); |
|
41
|
|
|
const data = res.scooters; |
|
42
|
|
|
setScooters(data); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
function updateMap() { |
|
46
|
|
|
const timer = setInterval(() => { |
|
47
|
|
|
if (timerRef.current === false) { |
|
48
|
|
|
clearInterval(timer); |
|
49
|
|
|
} else { |
|
50
|
|
|
fetchScooterData(); |
|
51
|
|
|
} |
|
52
|
|
|
}, 1000); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
const handleToggle = () => { |
|
56
|
|
|
setIsLive(!isLive); |
|
57
|
|
|
}; |
|
58
|
|
|
|
|
59
|
|
|
const handleSearch = async () => { |
|
60
|
|
|
const result = await getCoordinates(searchPhrase); |
|
61
|
|
|
const coordArr = [result.latitude, result.longitude]; |
|
62
|
|
|
setZoom(12); |
|
63
|
|
|
setFeatures(coordArr); |
|
64
|
|
|
}; |
|
65
|
|
|
|
|
66
|
|
|
const handleFlyToArea = async (place) => { |
|
67
|
|
|
const result = await getCoordinates(place); |
|
68
|
|
|
const coordArr = [result.latitude, result.longitude]; |
|
69
|
|
|
setZoom(12); |
|
70
|
|
|
setFeatures(coordArr); |
|
71
|
|
|
}; |
|
72
|
|
|
|
|
73
|
|
|
const handleFlyTo = (coords) => { |
|
74
|
|
|
setZoom(16); |
|
75
|
|
|
setFeatures(coords); |
|
76
|
|
|
}; |
|
77
|
|
|
|
|
78
|
|
|
return ( |
|
79
|
|
|
<div className="w-full"> |
|
80
|
|
|
{cityData ? ( |
|
81
|
|
|
<div className="flex flex-row w-full"> |
|
82
|
|
|
<div className="w-3/4"> |
|
83
|
|
|
<div className="absolute text-white z-100 leaflet-bottom pointer-events-auto bg-slate-700 p-3 rounded-tr-xl"> |
|
84
|
|
|
<label class="inline-flex relative items-center cursor-pointer"> |
|
85
|
|
|
<input |
|
86
|
|
|
onClick={(e) => { |
|
87
|
|
|
handleToggle(); |
|
88
|
|
|
}} |
|
89
|
|
|
type="checkbox" |
|
90
|
|
|
checked={isLive} |
|
91
|
|
|
class="sr-only peer" |
|
92
|
|
|
/> |
|
93
|
|
|
<div |
|
94
|
|
|
class="w-11 h-6 bg-gray-800 peer-focus:outline-none |
|
95
|
|
|
peer-focus:ring-4 peer-focus:ring-blue-300 |
|
96
|
|
|
dark:peer-focus:ring-blue-800 rounded-full peer |
|
97
|
|
|
dark:bg-slate-500 peer-checked:after:translate-x-full |
|
98
|
|
|
peer-checked:after:border-white after:content-[''] |
|
99
|
|
|
after:absolute after:top-[2px] after:left-[2px] |
|
100
|
|
|
after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 |
|
101
|
|
|
after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-blue-600" |
|
102
|
|
|
></div> |
|
103
|
|
|
<span class="ml-3 text-sm font-medium text-gray-900 dark:text-gray-300"> |
|
104
|
|
|
Live View |
|
105
|
|
|
</span> |
|
106
|
|
|
</label> |
|
107
|
|
|
</div> |
|
108
|
|
|
<Map |
|
109
|
|
|
center={center} |
|
110
|
|
|
zoom={zoom} |
|
111
|
|
|
features={features} |
|
112
|
|
|
scooters={scooters} |
|
113
|
|
|
cities={cityData} |
|
114
|
|
|
setIsLive={setIsLive} |
|
115
|
|
|
/> |
|
116
|
|
|
</div> |
|
117
|
|
|
<div className="w-1/4 flex flex-col max-h-screen bg-gray-800"> |
|
118
|
|
|
<div className="p-4"> |
|
119
|
|
|
<Searchbar |
|
120
|
|
|
searchPhrase={searchPhrase} |
|
121
|
|
|
setSearchPhrase={setSearchPhrase} |
|
122
|
|
|
handleSearch={handleSearch} |
|
123
|
|
|
/> |
|
124
|
|
|
</div> |
|
125
|
|
|
<div className="w-full overflow-scroll"> |
|
126
|
|
|
<MapSidebar |
|
127
|
|
|
scooters={scooters} |
|
128
|
|
|
cities={cityData} |
|
129
|
|
|
handleFlyTo={handleFlyTo} |
|
130
|
|
|
handleFlyToArea={handleFlyToArea} |
|
131
|
|
|
/> |
|
132
|
|
|
</div> |
|
133
|
|
|
</div> |
|
134
|
|
|
</div> |
|
135
|
|
|
) : ( |
|
136
|
|
|
<div> |
|
137
|
|
|
<h1>Loading</h1> |
|
138
|
|
|
</div> |
|
139
|
|
|
)} |
|
140
|
|
|
</div> |
|
141
|
|
|
); |
|
142
|
|
|
}; |
|
143
|
|
|
|
|
144
|
|
|
export default MapOverview; |
|
145
|
|
|
|